package cn.tedu._05weibo.controller;

import cn.tedu._05weibo.base.response.JsonResult;
import cn.tedu._05weibo.base.response.StatusCode;
import cn.tedu._05weibo.mapper.CommentMapper;
import cn.tedu._05weibo.pojo.dto.CommentDTO;
import cn.tedu._05weibo.pojo.entity.Comment;
import cn.tedu._05weibo.pojo.vo.CommentVO;
import cn.tedu._05weibo.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comments/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation("评论微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, HttpSession session) {
        // 获取当前登录用户的id
        UserVO userVO = (UserVO) session.getAttribute("user");
        // 创建Comment对象
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }
    @ApiOperation("根据微博id查询评论列表")
    @GetMapping("selectByWeiboId")
    public List<CommentVO> selectByWeiboId(Integer id) {
        System.out.println("id = " + id);
        return commentMapper.selectByWeiboId(id);
    }



}
